package com.foreknow.demo11;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by foreknow on 2021/7/25.
 */
public class Test {
    /**
     * 输出图书信息
     *
     * @param
     */
    public void printBooksInfo(List<Book> books) {

        Book b1 = new Book(1, "java", 33.45, 50);
        Book b2 = new Book(2, "jsp", 55.45, 150);
        Book b3 = new Book(3, "ssm", 77.45, 250);
        books.add(b1);
        books.add(b2);
        books.add(b3);

        for (int i = 0; i < books.size(); i++) {
            //for (Book book:books) {
            Book book = books.get(i);
            System.out.println(book.getBid() + "\t" + book.getBname() + "\t"
                    + book.getPrice() + "\t" + book.getStorage());
        }
    }

    /**
     * 购买图书
     *
     * @param
     */
    public Order buyBooks(List<Book> books) {
        Scanner input = new Scanner(System.in);
        // 1.创建一个订单（手动给一个订单编号）
        Order order = new Order("0001");
        // 2.循环3次接收用户输入
        List<OrderItem> items = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入图书的编号：");
            int pid = input.nextInt();
            System.out.println("请输入购买数量:");
            int pnum = input.nextInt();
            // 3.将购买的商品保存到数组中
            OrderItem item = new OrderItem(books.get(pid-1).getBname(),
                    books.get(pid - 1).getPrice(), pnum);
            items.add(item);
            order.setItems(items);
            System.out.println("请继续购买图书");
        }
        return order;
    }

    /**
     * 获取订单信息
     *
     * @param
     */

//    public void printOrderInfo(Order order) {
//
//    }

    public static void main(String[] args) {
        Test t = new Test();
        List<Book> books = new ArrayList<>();
        // 1.输出图书信息
         t.printBooksInfo(books);
/*
        Book b1 = new Book(1, "java", 33.45, 50);
        Book b2 = new Book(2, "jsp", 55.45, 150);
        Book b3 = new Book(3, "ssm", 77.45, 250);
*/

        // 2.购买图书
/*
         books[0] = new Book(1, "java", 33.45, 50);
        books[1] = new Book(2, "jsp", 55.45, 150);
        books[2] = new Book(3, "ssm", 77.45, 250);
*/

        Order order = t.buyBooks(books);


        // 3.输出订单信息
        // 3.1输出订单的编号
        System.out.println(order.getOrderId());
        // 3.2输出订单的总额
        System.out.println("订单的总额为" + order.getTotal());

        // 3.3输出商品列表中的内容
        List<OrderItem> itt = order.getItems();
        for(int i = 0;i<itt.size();i++) {
            OrderItem io = itt.get(i);
            System.out.println(io.getPname()+"--"+io.getPprice()+"--"+io.getNum());
        }


    }
}
